package com.hjm.project.GoodsInfo.GoodsInfo.controller;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import com.hjm.common.utils.fastdfs.FastDFSClient;
import com.hjm.framework.config.CustomConfig;
import com.hjm.project.BusinessInfo.BusinessInfo.domain.BusinessInfo;
import com.hjm.project.BusinessInfo.BusinessInfo.service.IBusinessInfoService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.hjm.framework.aspectj.lang.annotation.Log;
import com.hjm.framework.aspectj.lang.enums.BusinessType;
import com.hjm.project.GoodsInfo.GoodsInfo.domain.GoodsInfo;
import com.hjm.project.GoodsInfo.GoodsInfo.service.IGoodsInfoService;
import com.hjm.framework.web.controller.BaseController;
import com.hjm.framework.web.domain.AjaxResult;
import com.hjm.common.utils.poi.ExcelUtil;
import com.hjm.framework.web.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 商品Controller
 * 
 * @author GoodsInfo
 * @date 2020-11-30
 */
@Controller
@RequestMapping("/GoodsInfo/GoodsInfo")
public class GoodsInfoController extends BaseController
{
    private String prefix = "GoodsInfo/GoodsInfo";

    @Autowired
    private IGoodsInfoService goodsInfoService;

    @Autowired
    private IBusinessInfoService iBusinessInfoService;


    @RequiresPermissions("GoodsInfo:GoodsInfo:view")
    @GetMapping()
    public String GoodsInfo()
    {
        return prefix + "/GoodsInfo";
    }

    /**
     * 查询商品列表
     */
    @RequiresPermissions("GoodsInfo:GoodsInfo:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(GoodsInfo goodsInfo)
    {
        startPage();
        List<GoodsInfo> list = goodsInfoService.selectGoodsInfoList(goodsInfo);
        return getDataTable(list);
    }

    /**
     * 导出商品列表
     */
    @RequiresPermissions("GoodsInfo:GoodsInfo:export")
    @Log(title = "商品", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(GoodsInfo goodsInfo)
    {
        List<GoodsInfo> list = goodsInfoService.selectGoodsInfoList(goodsInfo);
        ExcelUtil<GoodsInfo> util = new ExcelUtil<GoodsInfo>(GoodsInfo.class);
        return util.exportExcel(list, "GoodsInfo");
    }

    /**
     * 新增商品
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存商品
     */
    @RequiresPermissions("GoodsInfo:GoodsInfo:add")
    @Log(title = "商品", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(GoodsInfo goodsInfo)
    {
        return toAjax(goodsInfoService.insertGoodsInfo(goodsInfo));
    }

   /**
    * @Description //上传商品(终端)信息
    * @Param [goodsInfo 商品信息, home(图片) , lbImg(轮播图片集合), dImg(商品详情图片集合) dImg(商品详情图片集合)]
    * @return com.hjm.framework.web.domain.AjaxResult
    **/
    @RequiresPermissions("GoodsInfo:GoodsInfo:add")
    @Log(title = "上传商品信息", businessType = BusinessType.INSERT)
    @PostMapping("/adds")
    @ResponseBody
    public AjaxResult addSaves(GoodsInfo goodsInfo,
                               @RequestParam("home") MultipartFile home,
                               @RequestParam("detailImg") List<MultipartFile> detailImg,
                               @RequestParam("lbImg") List<MultipartFile> lbImg,
                               @RequestParam("hdImg") List<MultipartFile> hdImg,
                               @RequestParam Map<String, MultipartFile> updateImg
                               ) throws IOException {
         //未收到表单数据
        if (goodsInfo == null){
            return error("错误的请求参数");
        }
        return toAjax(goodsInfoService.insertGoodsInfos(goodsInfo,home,detailImg,lbImg,hdImg,updateImg));
    }


    /**
     * @Description //修改商品信息
     * @Param [goodsInfo 商品信息, home(图片) , lbImg(轮播图片集合), dImg(商品详情图片集合)]
     * @return com.hjm.framework.web.domain.AjaxResult
     **/
    @RequiresPermissions("GoodsInfo:GoodsInfo:add")
    @Log(title = "修改图片", businessType = BusinessType.INSERT)
    @PostMapping("/updateGoods")
    @ResponseBody
    public AjaxResult updateGoodsInfo(GoodsInfo goodsInfo,
                                      @RequestParam MultipartFile home,
                                      @RequestParam List<MultipartFile> lbImg,
                                      @RequestParam List<MultipartFile> dImg,
                                      @RequestParam Map param) throws IOException {
        //未收到表单数据
        if (goodsInfo == null){
            return error("错误的请求参数");
        }
        return toAjax(goodsInfoService.updateGoodsInfo(goodsInfo,home,lbImg,dImg));
    }




    /**
     * 修改商品
     */
    @RequestMapping("/edit")
    public String edit(ModelMap mmap,GoodsInfo goodsInfos)
    {
        GoodsInfo goodsInfo = goodsInfoService.selectGoodsInfoById(Long.parseLong(goodsInfos.getId().toString()));
        mmap.put("goodsInfo", goodsInfo);
        return prefix + "/edits";
    }



    /**
     * 修改保存商品
     */
    @Log(title = "商品", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(GoodsInfo goodsInfo) {
        return toAjax(goodsInfoService.updateGoodsInfo(goodsInfo));
    }


    /**
     * 获取商品图片
     */
    @Log(title = "商品图", businessType = BusinessType.UPDATE)
    @PostMapping("/getImg")
    @ResponseBody
    public TableDataInfo getImg(GoodsInfo goodsInfo) {
        BusinessInfo businessInfo = new BusinessInfo();
        businessInfo.setPicId(goodsInfo.getId());
        List<BusinessInfo> businessInfos = iBusinessInfoService.selectBusinessInfoList(businessInfo);
        if (CollectionUtils.isNotEmpty(businessInfos)) {
            for (BusinessInfo info : businessInfos) {
                info.setUrl(CustomConfig.getImgUrl() + info.getUrl());
            }
        }
        return getDataTable(businessInfos);
    }


    /**
     * 删除商品
     * 删除商品会把商品的图片也一并删除
     * 上架商品删除时 必须下架
     */
    @RequiresPermissions("GoodsInfo:GoodsInfo:remove")
    @Log(title = "商品", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids,String state) {
        int i = goodsInfoService.deleteGoodsInfoByIds(ids);
        if (i > 0) {
            //删除图片服务器地址
            BusinessInfo businessInfo = new BusinessInfo();
            businessInfo.setPicId(Long.parseLong(ids));
            List<BusinessInfo> businessInfos = iBusinessInfoService.selectBusinessInfoList(businessInfo);
            //存在图片删除
            if (CollectionUtils.isNotEmpty(businessInfos)) {
                for (BusinessInfo info : businessInfos) {
                    FastDFSClient.deleteFile(info.getUrl());
                }
            }
            //删除库中的图片记录
            BusinessInfo bInfo = new BusinessInfo();
            bInfo.setPicId(Long.parseLong(ids));
            iBusinessInfoService.deleteBusinessInfoByName(bInfo);
        }
        return toAjax(i);
    }


    /**
     * 删除商品图片
     */
    @RequiresPermissions("GoodsInfo:GoodsInfo:remove")
    @Log(title = "删除商品图片", businessType = BusinessType.DELETE)
    @PostMapping( "/delImg")
    @ResponseBody
    public AjaxResult delImg(@RequestParam String goodsId,@RequestParam String type,@RequestParam String url) {
        BusinessInfo businessInfo = new BusinessInfo();
        businessInfo.setPicId(Long.parseLong(goodsId));
        businessInfo.setUrl(url);
        if (type.equals("detail")) {
            businessInfo.setType(Long.parseLong("3"));
        }
        if (type.equals("lb")){
            businessInfo.setType(Long.parseLong("2"));
        }
        if (type.equals("hd")){
            businessInfo.setType(Long.parseLong("11"));
        }
        return toAjax(goodsInfoService.delGoodsImg(businessInfo));
    }

}
